package com.thinvent.nj.codecounter.service;

import org.springframework.util.Assert;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
/**
 * Created by Liupj on 2015/3/4.
 */
public class CounterService {

    public void unZipFile(String zipFilePath, String target) {
        Assert.notNull(zipFilePath, "zipFilePath cannot be null.");

        ZipInputStream zipInputStream = null;
        BufferedOutputStream os = null;
        InputStream is = null;

        try {
            //压缩文件
            File file = new File(zipFilePath);

            //实例化ZipFile，每一个zip压缩文件都可以表示为一个ZipFile
            ZipFile zipFile = new ZipFile(file, ZipFile.OPEN_READ, Charset.forName("GBK"));

            //实例化一个Zip压缩文件的ZipInputStream对象，可以利用该类的getNextEntry()方法依次拿到每一个ZipEntry对象
            zipInputStream = new ZipInputStream(new FileInputStream(file), Charset.forName("GBK"));
            ZipEntry zipEntry = null;

            String fileName = file.getName();

            File tempDir = new File(target, fileName.substring(0, fileName.lastIndexOf(".")));

            if (!tempDir.exists()) {
                tempDir.mkdirs();
            }


            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                if (zipEntry.isDirectory()) {
                    System.out.println("is directory.");
                    continue;
                }


                System.out.println("is file, fileName = " + zipEntry.getName());

                String itemFileName = zipEntry.getName();
                File temp = new File(tempDir, itemFileName);
                if (! temp.getParentFile().exists()) {
                    temp.getParentFile().mkdirs();
                }

                temp.createNewFile();

                os = new BufferedOutputStream(new FileOutputStream(temp));
                //通过ZipFile的getInputStream方法拿到具体的ZipEntry的输入流
                is = zipFile.getInputStream(zipEntry);


                if (is == null) {
                    System.out.println(".....................");
                }

                int len = 0;

                byte[] b = new byte[1024];

                while ((len = is.read(b)) != -1) {
                    os.write(b, 0, len - 1);
                }

                os.flush();
                os.close();
                is.close();
            }

        } catch (IOException e) {
            throw new IllegalStateException(e);
        } finally {
            try {
                if (os != null) {
                    os.close();
                }

            } catch (IOException e) {
                throw new IllegalStateException("os cannot be closed.", e);
            } finally {
                os = null;
            }


            try {
                if (is != null) {
                    is.close();
                }

            } catch (IOException e) {
                throw new IllegalStateException("is cannot be closed.", e);
            } finally {
                is = null;
            }

            try {
                if (zipInputStream != null) {
                    zipInputStream.close();
                }

            } catch (IOException e) {
                throw new IllegalStateException("zipInputStream cannot be closed.", e);
            } finally {
                zipInputStream = null;
            }
        }

    }



    public static void main(String args[]) throws IOException {
       new CounterService().unZipFile("D:\\cssAll-view.war", "D:\\unpack");


    }

}
